package querysrc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class querydb {
	public static class WRAPPER
	{
		public querydb qdb = null;
		public WRAPPER(querydb q)
		{
			qdb = q;
		}
	}
	Connection con = null;
	PreparedStatement ps = null;	
	String sql = "SELECT mac,email FROM foodbase.users where user_id=?";
	public querydb(Connection c, String sqlsen)
	{
		con = c;		
		sql = sqlsen;
	}
	
	/*
	public boolean setQuery(String sqlsen)
	{
		sql = sqlsen;
		return true;
	}
	*/
	public boolean prePS()
	{
		try
		{
			ps = con.prepareStatement(sql);
			return true;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return false;
	    }
	}
	
	public PreparedStatement getPS()
	{
		return ps;
	}
	
	public ResultSet execPSQuery()
	{
		try
		{
			ResultSet rs = ps.executeQuery();
			return rs;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return null;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public int execPSUpdate()
	{
		try
		{
			int iret = ps.executeUpdate();
			return iret;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
	}
	
	public int execPSInsert()
	{
		try
		{
			boolean bret = ps.execute();
			
			return 0;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
	}
	
	public void finalize()
	{
		try
		{
			if(ps!=null)
			{
				ps.close();
				ps = null;
			}
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());		      
	    }
	}

}
